package com.ys.erp.stage_user_service.listener;

import com.alibaba.fastjson2.JSONArray;
import com.ys.erp.entity.data.bean.StrongComplaint;
import com.ys.erp.entity.data.bean.StrongComplaintRecord;
import com.ys.erp.entity.service.IStrongComplaintRecordService;
import com.ys.erp.stage_user_service.bean.event.UserComplaintEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author lwj
 * @date 2025/7/22
 */
@Component
@Slf4j
@RefreshScope
@RequiredArgsConstructor
public class UserComplaintListener {

    private final IStrongComplaintRecordService iStrongComplaintRecordService;

    @Async
    @EventListener(classes = {UserComplaintEvent.class})
    public void complaint(UserComplaintEvent event) {
        StrongComplaint strongComplaint = event.getStrongComplaint();
        StrongComplaintRecord record = new StrongComplaintRecord();
        record.setComplaintNo(strongComplaint.getComplaintNo());
        record.setRecordType(strongComplaint.getComplaintStatus());
        record.setDisImage(JSONArray.toJSONString(event.getImageList()));
        if (StrongComplaint.Enums.DIS_START.name().equals(strongComplaint.getComplaintStatus())) {
            record.setDisDescribe(strongComplaint.getDisEmpName() + " 接入处理");
        }
        if (StrongComplaint.Enums.DIS_OK.name().equals(strongComplaint.getComplaintStatus())) {
            record.setDisDescribe(strongComplaint.getDisEmpName() + " 已完成处理");
        }
        record.setRecordTime(LocalDateTime.now());
        iStrongComplaintRecordService.save(record);
    }

}
